<?php

namespace App\Hospital\Controllers;

use App\Hospital\Metrics\Examples;
use App\Http\Controllers\Controller;
use App\Models\HospitalAdminUserHospital;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Widgets\Card;

class HomeController extends AdminController
{
    public function index(Content $content)
    {


        $adminHospitalId = request()->input('admin_hospital_id');

        $header = "";
        if($adminHospitalId){
            try {
                $adminUser = Admin::user();

                $hospitals = HospitalAdminUserHospital::query()->where('hospital_admin_users_id',$adminUser->id)->pluck('hospital_id')->toArray();
                if(in_array($adminHospitalId,$hospitals)){
                    $adminUser->hospital_id = $adminHospitalId;
                    $adminUser->save();
                }else{
                    $alert = Alert::make('所选社区不存在', '失败');
                    $header = $alert->warning();

                }
            }catch (\Exception $e){
                return $this->failed('失败'.$e->getMessage());

            }
        }
        return $content
            ->header('仪表盘')
            //->description('Description...')
            ->body(function (Row $row) use ($header) {
                $row->column(12,$header);
                $row->column(12, function (Column $column) {
                    $column->row(function (Row $row) {
                        $row->column(6, Dashboard::title());
                        $row->column(3, new Examples\SignTotle());
                        $row->column(3, new Examples\SignProportion());
                    });
                });
                $row->column(12, function (Column $column) {
                    $column->row(function (Row $row) {
                        $hospital_id = Admin::user()->hospital_id;

                        $row->column(3, new Card('36月龄中医药儿童健康管理',view('admin.index_data',Examples\IndexData::child($hospital_id))));
                        $row->column(3, new Card('0-6岁儿童家庭医生管理',view('admin.index_data',Examples\IndexData::bigChild($hospital_id))));
                        $row->column(3, new Card('孕产妇家庭医生管理',view('admin.index_data',Examples\IndexData::preg($hospital_id))));
                        $row->column(3, new Card('宣教',view('admin.index_data',Examples\IndexData::guide($hospital_id))));
                        $row->column(12,new Card("除今日数据外，其余需要同步数据后更新"));

                    });
                });
            });
    }
}
